package com.audaque.springboot.foshanupload.sessionvalid.filter;


import cn.hutool.core.bean.BeanUtil;
import com.audaque.springboot.foshanupload.authcore.constants.AuthConst;
import com.audaque.springboot.foshanupload.authcore.model.currentUser.TransmittableThreadLocalCurrentUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


@Slf4j
public class SessionFilter implements Filter {


    private List<String> excludedUris;
    private String applicationContextPath;

    public void init(FilterConfig filterConfig) throws ServletException {
        String paramExcludedUris = filterConfig.getInitParameter("excludedUris");//获取参数值
        if (StringUtils.isNotBlank(paramExcludedUris)) {
            this.excludedUris = new ArrayList<String>(Arrays.asList(paramExcludedUris.split(","))) ;
        }

        String paramApplicationContextPath= filterConfig.getInitParameter("applicationContextPath");//获取参数值
        if (StringUtils.isNotBlank(paramApplicationContextPath)) {
            this.applicationContextPath = paramApplicationContextPath;
        }


    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        String requestUri = httpServletRequest.getRequestURI();
        String replace = requestUri.replace(applicationContextPath, "");
        // 检查请求的 URI 是否在排除列表中
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        for (String excludedUri : excludedUris) {
            if (antPathMatcher.match(excludedUri, replace)) {
                // 如果是排除的 URI，直接继续到下一个过滤器
                filterChain.doFilter(request, response);
                return;
            }
        }
        //int i=1/0;

        //spring session负责创建sessionId，不用开发者处理
        HttpSession session = httpServletRequest.getSession();
        //没有则放行
        if (session == null) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }
        String sessionId = session.getId();
        //有状态
        Object attribute = session.getAttribute(AuthConst.CURRENT_USER_KEY);
        if (attribute != null) {
            TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = BeanUtil.toBean(attribute, TransmittableThreadLocalCurrentUser.class);
            String sessionId1 = transmittableThreadLocalCurrentUser.getSessionId();
            if (Objects.equals(sessionId, sessionId1)) {
                //添加当前用户到上下文
                TransmittableThreadLocalCurrentUser.setCurrentUser(transmittableThreadLocalCurrentUser);
            }
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        return;
    }

}
